﻿using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using WinnerCMS.Common;
using WinnerCMS.BLL.Rights;
namespace WinnerCMS.WebFactory
{
    /// <summary>
    /// 后台专用ASPX页面的基类
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {


        #region 私有字段

        private string _emptyUc = "";
        //protected Stopwatch watch;
        //protected long RuntimeToal;
        #endregion

        #region 属性

        /// <summary>
        /// 是否压缩
        /// </summary>
        public bool CompressViewState { get; set; }

        /// <summary>
        /// 空数据的用户控件地址
        /// </summary>
        public string EmptyUC
        {
            set
            {
                _emptyUc = value;
            }
            get
            {
                return _emptyUc;
            }
        }
        /// <summary>
        /// 程序物理路径
        /// </summary>
        public string ApplicationPath
        {
            get
            {
                string appPath = HttpContext.Current.Request.ApplicationPath;
                if (appPath != null && appPath.Trim() == "/")
                    return "";
                return appPath;
            }
        }
        //返回图片路径
        protected String ImageRoot
        {
            get
            {
                return HttpContext.Current.Request.ApplicationPath + "/Images/";
            }
        }
        //系统路径
        protected string RootPath
        {
            get
            {
                return Request.ApplicationPath;
            }
        }

        /// <summary>
        /// 是否执行绑定空数据模板
        /// </summary>
        protected bool IsBindEmptyData { get; set; }

        #endregion

        #region 重写事件
        protected override void OnInit(EventArgs e)
        {
            //watch = new System.Diagnostics.Stopwatch();
            //watch.Start();
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent()
        {
            this.Load += this.Page_Load;
            this.Error += this.Page_Error;
            this.PreRenderComplete += this.Page_PreRenderComplete;
        }

        #endregion

        private readonly static RightsControlManage Rrm = new RightsControlManage();
        private void Page_Load(object sender, System.EventArgs e)
        {
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
        }

        private void Page_PreRenderComplete(object sender, System.EventArgs e)
        {
            Rrm.SetAllControl(this);
            //watch.IsStop();
            //RuntimeToal = watch.ElapsedMilliseconds;
        }


        #region Page_Error
        private void Page_Error(object sender, System.EventArgs e)
        {
            //发生未处理异常

        }
        #endregion

        #region 重写Render
        //protected override void Render(HtmlTextWriter writer)
        //{
        //    TextWriter tempWriter = new StringWriter();
        //    base.Render(new HtmlTextWriter(tempWriter));
        //    StringBuilder Html = new StringBuilder(tempWriter.ToString());

        //    //定义页面的应用js，所有从这个基类继承而来的页面都不需要添加js的链接。
        //    string[] js = new string[] { "/JS/JQ/jquery.js" ,
        //        "/JS/JQ/jquery.dragndrop.js",
        //        "/JS/JQ/jquery.msgbox.js",
        //        "/JS/JQ/jquery.validate.js"
        //    };
        //    //string[] js = new string[0];

        //    string backgroundpath = ApplicationPath;
        //    StringBuilder jsstring = new StringBuilder();
        //    foreach (string str in js)
        //    {
        //        jsstring.AppendFormat("<script src=\"{0}{1}\" type=\"text/javascript\"></script>\n", ApplicationPath, str);
        //    }

        //    int scriptIndex = Html.ToString().IndexOf("<script", StringComparison.CurrentCultureIgnoreCase);
        //    if (scriptIndex > -1)
        //    {
        //        Html.Insert(scriptIndex - 1, jsstring);
        //        writer.Write(Html);
        //    }
        //    else
        //    {
        //        jsstring.Insert(0, "<head>");
        //        writer.Write(Html.Replace("<head>", jsstring.ToString()));
        //    }
        //}
        #endregion


        #region 页面提交时是否显示进度条

        public PageBase()
        {
            EnableProgressBar = false;
            IsBindEmptyData = false;
            CompressViewState = false;
        }

        /// <summary>
        /// 页面提交时是否显示进度条
        /// </summary>
        public bool EnableProgressBar { get; set; }

        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();


            //Page.ClientScript.RegisterStartupScript(Page.GetType(), "PostProgress", "<script language='javascript' src='/ShowProgressInfo.js'></script>");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (EnableProgressBar)
            {
                foreach (Control c in this.Controls)
                {
                    if (c is HtmlForm)
                    {
                        HtmlForm form1 = (HtmlForm)c;

                        form1.Attributes.Add("onsubmit", "ShowProgressInfo1('正在等待服务器响应，请稍等！');"); //添加进度条 

                        return;
                    }
                }
            }
        }
        #endregion

        public static ITemplate GetTemplate(Page page)
        {
            return page.LoadTemplate("~/UserControl/UC_EmptyDataTemplate.ascx");
        }
    }
}